package com.cuanbobox.utils;

import android.util.Log;

import com.cuanbobox.activity.MainActivity;
import com.cuanbobox.bean.PPTBean;
import com.cuanbobox.bean.WdsBean;
import com.cuanbobox.bean.WdsDataList;
import com.cuanbobox.constant.CbConstant;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class WdsThread extends Thread{
    private String str;
    public WdsThread(String data){
        this.str=data;
    }

    @Override
    public void run() {
        super.run();
        try{
            if(Pattern.matches("<SLT:.*:.*:.*\\.", str)){
                str=str.substring(0,str.length()-1);
                String[] sAry=str.split(":");
                bindData(sAry);
                LogUtil.add("接收数据",""+str,"互动TCP接收过滤数据");
            }
            if(Pattern.matches("<PPTSLT:Start:.*\\.", str)){
                MainActivity.pptLst.removeAll(MainActivity.pptLst);
                str=str.substring(0,str.length()-1);
                String[] sAry=str.split(":");
                MainActivity.sumPage=Integer.valueOf(sAry[2]);
                BroadcastUtil.sendWdsBean(CbConstant.PPT_START);
                LogUtil.add("接收数据",""+str,"互动TCP接收过滤数据");
            }
            if(Pattern.matches("<PPTSLT:.*:.*\\.", str)){
                str=str.substring(0,str.length()-1);
                String[] sAry=str.split(":");
                PPTBean pptBean=new PPTBean(Integer.valueOf(sAry[1]),sAry[2]);
                boolean have=false;
                for (int j = 0; j< MainActivity.pptLst.size(); j++){
                    PPTBean bean=MainActivity.pptLst.get(j);
                    if(bean.getPage()==pptBean.getPage()){
                        bean.setBase64img(pptBean.getBase64img());
                        have=true;
                    }
                }
                if(!have){
                    MainActivity.pptLst.add(pptBean);
                }
                LogUtil.add("接收数据",""+str,"互动TCP接收过滤数据");
            }
            if(Pattern.matches("<PPT_Note:.*\\.", str)){
                str=str.substring(0,str.length()-1);
                MainActivity.pptNote=str.split(":");
                BroadcastUtil.sendWdsBean(CbConstant.PPT_NOTE);
                LogUtil.add("接收数据",""+str,"互动TCP接收过滤数据");
            }
            if(Pattern.matches("<PPT_CurrentPage:.*\\.", str)){
                str=str.substring(0,str.length()-1);
                String[] sAry=str.split(":");
                MainActivity.nowPage=Integer.valueOf(sAry[1]);
                BroadcastUtil.sendWdsBean(CbConstant.PPT_NOWPAGE);
                LogUtil.add("接收数据",""+str,"互动TCP接收过滤数据");
            }
            if((str.equals("<PPTSLT:End."))){
                BroadcastUtil.sendWdsBean(CbConstant.PPT_BEAN);
                WdsDataList.add(">PPT_Note.");
                LogUtil.add("接收数据",""+str,"互动TCP接收过滤数据");
            }
            if((str.equals("<PPT:End."))){
                BroadcastUtil.sendWdsBean(CbConstant.PPT_END);
                LogUtil.add("接收数据",""+str,"互动TCP接收过滤数据");
            }
        }catch (Exception e){
            LogUtil.addError("----------------WdsThread处理数据时异常-----------------");
            LogUtil.addError(e);
            LogUtil.addError("----------------end-----------------");
        }
    }

    private void bindData(String[] sAry){
        if(sAry[2].equals("设置")||sAry[2].equals("Windows 输入体验"))return;
        boolean have=false;
        for (int i=0;i<MainActivity.wdslst.size();i++){
            WdsBean wdsBean=MainActivity.wdslst.get(i);
            if(wdsBean.getName().equals(sAry[1])){
                have=true;
                wdsBean.setBase64Img(sAry[3]);
                wdsBean.setRecordTime(new Date().getTime());
            }
        }
        if(!have){
            MainActivity.wdslst.add(new WdsBean(sAry[1],sAry[2],sAry[3],new Date().getTime()));
        }
        List<WdsBean> removeList=new ArrayList<>();
        for (int i=0;i<MainActivity.wdslst.size();i++){
            WdsBean wdsBean=MainActivity.wdslst.get(i);
            if(wdsBean.getName().equals("返回桌面")){
                continue;
            }
            long diff=new Date().getTime()-wdsBean.getRecordTime();
            if(diff>10000){
                removeList.add(wdsBean);
            }
        }
        MainActivity.wdslst.removeAll(removeList);
/*        if(MainActivity.wdslst.size()>0){
            if(!MainActivity.wdslst.get(0).getName().equals("返回桌面")){
                MainActivity.wdslst.add(0,new WdsBean("返回桌面","返回桌面","",0));
            }
        }else{
            MainActivity.wdslst.add(0,new WdsBean("返回桌面","返回桌面","",0));
        }*/
        BroadcastUtil.sendWdsBean(CbConstant.WDS_BEAN);
    }
}
